<?php
/**
 * Pages_InstructorController
 *
 * Working with teachers
 */

class Pages_InstructorController extends Zend_Controller_Action 
{
    /**
     * Show group list for current teacher
     *
     */
    public function indexAction() 
    {
        $this->checkAuth();
        $tableInstructor = new Zend_Instructor();
        $instId = $this->_getParam('instId');
        if ($this->_getParam('instYear') != NULL)
        $instYear = $this->_getParam('instYear');
        else 
        {
            $today = getdate();
            $month=$today['mon'];
            $year=$today['year'];
            if ($month > 8)
            $instYear=$year;
            else
            $instYear=$year-1;
        }
        $groups = $tableInstructor->getGroups($instId, $instYear);
        if ($groups != NULL) 
        {
            for ($i=0; $i<count($groups); $i++) 
            {
                $g_id=$groups[$i]['ID'];
                $g_name[]=$groups [$i]['NAME'];
                $group_links[]=BASE_URL . "/pages/instructor/$instId/group/$g_id";
            }
            $this->view->groups=$group_links;
            $this->view->g_name=$g_name;
        }
        else 
        $this->view->message='Немає жодної групи';
        $this->getInstructor($instId);
        $this->view->instId=$instId;
    }

    /**
     * Show students from current group
     *
     */
    public function groupAction() 
    {
        $this->checkAuth();
        $tableInstructor = new Zend_Instructor();
        $instId = $this->_getParam('instId');
        $groupId = $this->_getParam('groupId');
        $today = getdate();
        $month=$today['mon'];
        $year=$today['year'];
        if ($month > 8)
        $instYear=$year;
        else
        $instYear=$year-1;
        $this->getGroups($instId, $instYear, $groupId);
        $this->getStud($groupId, $instId);
        $this->getInstructor($instId);
        $this->view->instId=$instId;
        $this->view->groupId=$groupId;
    }

    /**
     * Choosing task from available ones
     *
     */
    public function studentchooseAction() 
    {
        $this->checkAuth();
        $tableInstructor = new Zend_Instructor();
        $instId = $this->_getParam('instId');
        $studentId = $this->_getParam('studentId');
        $tableStudent = new Zend_Student();
        $student = $tableStudent->getStudent($studentId);
        $taskId = (int)$this->_getParam('taskId');
        if ($taskId != NULL)
        {
            $check=$tableStudent->insertTask($studentId, $taskId);
            if ($check == 1)
            $this->_redirect('/pages/instructor/' . $instId . '/student/' . $studentId);
            else echo 'Помилка бази даних';
        }
        else
        {
            if ($student != NULL) 
            {
                $this->view->studentId = $studentId;
                $groupId=$student['GROUP_ID'];
                $tasks=$tableStudent->availableTasks($groupId);
                $this->view->tasks=$tasks;
                $this->view->instId=$instId;
                $this->getStud($groupId, $instId);
                $this->getGroups($instId, $instYear, $groupId);
                $this->getInstructor($instId);
            }
            else 
            $this->view->message='Даного студента не знайдено.';
        }
    }

    /**
     * Show student's task
     *
     */
    public function studentviewAction()
    {
        $this->checkAuth();
        $tableInstructor = new Zend_Instructor();
        $instId = $this->_getParam('instId');
        $studentId = $this->_getParam('studentId');
        $tableStudent = new Zend_Student();
        $student = $tableStudent->getStudent($studentId);
        if ($student != NULL) 
        {
            $groupId=$student['ID'];
            $task = $tableStudent->checkStudent($studentId);
            $taskId = $task['ID'];
            $task_numbers = $tableStudent->getTask($taskId);
            $this->view->n1 = $task_numbers[0] ['name'];
            $this->view->n2 = $task_numbers[1] ['name'];
            $this->view->img1 = $task_numbers [0] ['image'];
            $this->view->img2 = $task_numbers [1] ['image'];
            $this->view->v1 = $task_numbers[0] ['var'];
            $this->view->v2 = $task_numbers[1] ['var'];
            $this->view->vd1 = $task_numbers [0] ['var_data'];
            $this->view->vd2 = $task_numbers [1] ['var_data'];
        }
        $this->getInstructor($instId);
        $this->view->instId=$instId;
        $this->getStud($groupId, $instId);
        $this->getGroups($instId, $instYear, $groupId);
    }

    /**
     * Compare auth credentials, getting from form with credentials from DB
     * If it's ok - set auth session
     *
     */
    public function loginAction() 
    {
        $form = new Zend_LoginInstructor();
        $form->submit->setLabel('Login');
        $this->view->form = $form;
        if ($this->_request->isPost()) 
        {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) 
            {
                $login=$form->getValue('login');
                $pass=md5($form->getValue('pass'));
                $tableInstructor = new Zend_Instructor();
                $pass_ = $tableInstructor->checkPass($login, $pass);
                if ($pass_ != NULL) 
                {
                    $auth = new Zend_Session_Namespace('auth');
                    $auth->login = $login;
                    $auth->pass = $pass;
                    $instId=$pass_['TEACH_ID'];
                    $this->_redirect('/pages/instructor/' . $instId);
                }
                else echo '<center>Невірні логін/пароль</center>';
            }
            else
            {
                $form->populate($formData);
            }
        }
    }

    /**
     * Unset auth session
     */
    public function logoutAction() 
    {
        $auth = new Zend_Session_Namespace('auth');
        $auth->login = '';
        $auth->pass = '';
        $this->_redirect('/pages/instructor/login');
    }

    /**
     * Get students data and set View variables
     * 
     * @param int $groupId
     * @param int $instId
     */
    private function getStud($groupId, $instId)
    {
        $tableInstructor = new Zend_Instructor();
        $students = $tableInstructor->getStudents($groupId);
        if ($students != NULL)
        {
            $this->view->message='';
            for ($i=0; $i<count($students); $i++) 
            {
                $stud_id=$students[$i]['ID'];
                $stud_name[]=$students[$i]['NAME_LAST'] . ' ' . $students[$i]['NAME_FIRST'] . ' ' . $students[$i]['NAME_MIDDLE'];
                $check[] = $tableInstructor->checkStudent($stud_id);
                if ($tableInstructor->checkStudent($stud_id) == 'No')
                $stud_links[]=BASE_URL . "pages/instructor/$instId/studtask/$stud_id";
                else
                $stud_links[]=BASE_URL . "pages/instructor/$instId/student/$stud_id";
            }
            $this->view->stud=$stud_links;
            $this->view->stud_name=$stud_name;
            $this->view->check=$check;
        }
        else 
        $this->view->message='Немає жодного студента';
    }

    /**
     * Get groups data and set View variables
     * 
     * @param int $instId
     * @param int $instYear
     * @param int $groupId
     */
    private function getGroups($instId, $instYear, $groupId) 
    {
        $tableInstructor = new Zend_Instructor();
        $groups = $tableInstructor->getGroups($instId, $instYear);
        if ($groups != NULL) 
        {
            for ($i=0; $i<count($groups); $i++) 
            {
                $g_id=$groups[$i]['ID'];
                if ($g_id == $groupId)
                $this->view->active_group=$i;
                $g_name[]=$groups [$i]['NAME'];
                $group_links[]=BASE_URL . "/pages/instructor/$instId/group/$g_id";
            }
            $this->view->groups=$group_links;
            $this->view->g_name=$g_name;
        }
    }

    /**
     * Get teacher data and set View variable
     * 
     * @param  int $instId 
     *
     */
    private function getInstructor($instId) 
    {
        $tableInstructor = new Zend_Instructor();
        $inst = $tableInstructor->getInst($instId);
        if ($inst != NULL) 
        {
            $allInst=$inst[0]['LAST_NAME'] . ' ' . $inst[0]['FIRST_NAME'] . ' ' . $inst[0]['MIDDLE_NAME'];
            $this->view->Inst=$allInst;
        }
    }

    /**
     * Check if auth credentials are true and active
     */
    private function checkAuth() 
    {
        $tableInstructor = new Zend_Instructor();
        $auth = new Zend_Session_Namespace('auth');
        if ((!isset($auth->login) || !isset($auth->pass)) || ($tableInstructor->checkPass($auth->login, $auth->pass)) == NULL)
        $this->_redirect('/pages/instructor/login');
    }
}
